<HTML>
<HEAD>
<TITLE>[Chapter 10] 10.5 Menu</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:46:25 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch10_04.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 10<br>Would You Like to Choose from the Menu?</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch10_06.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-10-SECT-5">10.5 Menu</A></h2>

<P CLASS=para>
<A NAME="CH10.MENU10"></A><tt CLASS=literal>Menu</tt>s are the pull-down objects 
that appear on the <tt CLASS=literal>MenuBar</tt> 
of a <tt CLASS=literal>Frame</tt> or within other 
menus. They contain <tt CLASS=literal>MenuItem</tt>s 
or <tt CLASS=literal>CheckboxMenuItem</tt>s for the 
user to select. The <tt CLASS=literal>Menu</tt> class 
subclasses <tt CLASS=literal>MenuItem</tt> (so it 
can appear on a <tt CLASS=literal>Menu</tt>, too) 
and implements <tt CLASS=literal>MenuContainer</tt>. 
Tear-off menus are menus that can be dragged, placed elsewhere on the 
screen, and remain on the screen when the input focus moves to something 
else. Java supports tear-off menus if the underlying platform does. Motif 
(UNIX) supports tear-off menus; Microsoft Windows platforms do not. 

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-10-SECT-5.1">Menu Methods</A></h3>Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Menu () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The first constructor for <tt CLASS=literal>Menu</tt> 
creates a menu that has no label and cannot be torn off. To set the label 
at a later time, use <tt CLASS=literal>setLabel()</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Menu (String label) </I><br>
<DD>

<P CLASS=para>
This constructor for <tt CLASS=literal>Menu</tt> creates 
a <tt CLASS=literal>Menu</tt> with <tt CLASS=literal>label</tt> 
displayed on it. The <tt CLASS=literal>Menu</tt> cannot be torn off. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Menu (String label, boolean tearOff) </I><br>
<DD>

<P CLASS=para>
This constructor for <tt CLASS=literal>Menu</tt> creates 
a <tt CLASS=literal>Menu</tt> with <tt CLASS=literal>label</tt> 
displayed on it. The handling of <tt CLASS=literal>tearOff</tt> 
is platform dependent. 

<P CLASS=para>
<A HREF="ch10_05.htm#JAWT-CH-10-FIG-3">Figure 10.3</A> shows a tear-off menu for Windows NT/95 and Motif. Since Windows does not 
support tear-off menus, the Windows menu looks and acts like a regular 
menu. </DL>
<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="JAWT-CH-10-FIG-3">Figure 10.3: Tear-off menu</A></h4>


<p>
<img align=middle src="./figs/jawt1003.gif" alt="[Graphic: Figure 10-3]" width=503 height=184 border=0>

</DIV>

Items

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public int getItemCount() <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br><I CLASS=emphasis>public int countItems () <img src="gifs/wstar.gif" alt="(Deprecated)" border=0></I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getItemCount()</tt> method returns 
the number of items within the <tt CLASS=literal>Menu</tt>. 
Only top-level items are counted: if an item is a submenu, this method 
doesn't include the items on it. 

<P CLASS=para>
<tt CLASS=literal>countItems()</tt> is the Java 1.0 
name for this method. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public MenuItem getItem (int index) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getItem()</tt> method returns 
the <tt CLASS=literal>MenuItem</tt> at position <tt CLASS=literal>index</tt>. 
If <tt CLASS=literal>index</tt> is invalid, <tt CLASS=literal>getItem()</tt> 
throws the <tt CLASS=literal>ArrayIndexOutOfBoundsException</tt> run-time exception. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized MenuItem add (MenuItem item) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>add()</tt> method puts <tt CLASS=literal>item</tt> 
on the menu. The label assigned to <tt CLASS=literal>item</tt> 
when it was created is displayed on the menu. If <tt CLASS=literal>item</tt> 
is already in another menu, it is removed from that menu. If <tt CLASS=literal>item</tt> 
is a <tt CLASS=literal>Menu</tt>, it creates a submenu. 
(Remember that <tt CLASS=literal>Menu</tt> subclasses 
<tt CLASS=literal>MenuItem</tt>.) 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void add (String label) </I><br>
<DD>

<P CLASS=para>
This version of <tt CLASS=literal>add()</tt> creates 
a <tt CLASS=literal>MenuItem</tt> with <tt CLASS=literal>label</tt> 
as the text and adds that to the menu. If <tt CLASS=literal>label</tt> 
is the <tt CLASS=literal>String</tt> "-", 
a separator bar is added to the <tt CLASS=literal>Menu</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void insert(MenuItem item, int index) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>insert()</tt> method puts <tt CLASS=literal>item</tt> 
on the menu at position <tt CLASS=literal>index</tt>. 
The label assigned to <tt CLASS=literal>item</tt> 
when it was created is displayed on the menu. Positions are zero based, 
and if <tt CLASS=literal>index</tt> &lt; 0, <tt CLASS=literal>insert()</tt> 
throws the <tt CLASS=literal>IllegalArgumentException</tt> run-time exception. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void insert(String label, int index) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
This version of <tt CLASS=literal>insert()</tt> method 
creates a <tt CLASS=literal>MenuItem</tt> with <tt CLASS=literal>label</tt> 
as the text and adds that to the menu at position <tt CLASS=literal>index</tt>. 
If <tt CLASS=literal>label</tt> is the String "--", 
a separator bar is added to the <tt CLASS=literal>Menu</tt>. 
Positions are zero based, and if <tt CLASS=literal>index</tt> 
&lt; 0, this method throws the <tt CLASS=literal>IllegalArgumentException</tt> run-time exception. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void addSeparator () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>addSeparator()</tt> method creates 
a separator <tt CLASS=literal>MenuItem</tt> and adds 
that to the menu. Separator menu items are strictly cosmetic and do not generate events when selected. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void insertSeparator(int index) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>insertSeparator()</tt> method 
creates a separator <tt CLASS=literal>MenuItem</tt> 
and adds that to the menu at position <tt CLASS=literal>index</tt>. 
Separator menu items are strictly cosmetic and do not generate events when selected. Positions are zero based. If <tt CLASS=literal>index</tt> 
&lt; 0, <tt CLASS=literal>insertSeparator()</tt> throws 
the <tt CLASS=literal>IllegalArgumentException</tt> run-time exception. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void remove (int index) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>remove()</tt> method removes 
the <tt CLASS=literal>MenuItem</tt> at position <tt CLASS=literal>index</tt> 
from the <tt CLASS=literal>Menu</tt>. If <tt CLASS=literal>index</tt> 
is invalid, <tt CLASS=literal>remove()</tt> throws 
the <tt CLASS=literal>ArrayIndexOutOfBoundsException</tt> run-time exception. 
<tt CLASS=literal>index</tt> is zero based, so it 
can range from 0 to <tt CLASS=literal>getItemCount()-1</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void remove (MenuComponent component) </I><br>
<DD>

<P CLASS=para>
This version of <tt CLASS=literal>remove()</tt> removes 
the menu item <tt CLASS=literal>component</tt> from 
the <tt CLASS=literal>Menu</tt>. If component is not in the <tt CLASS=literal>Menu</tt>, 
nothing happens. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void removeAll() </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>removeAll()</tt> removes all 
<tt CLASS=literal>MenuItems</tt> from the <tt CLASS=literal>Menu</tt>. </DL>
Peers

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void addNotify ()  </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>addNotify()</tt> method creates 
the <tt CLASS=literal>Menu</tt> peer with 
all the <tt CLASS=literal>MenuItems</tt> on it. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void removeNotify ()  </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>removeNotify()</tt> method destroys 
the peer of the <tt CLASS=literal>MenuComponent</tt> 
and removes it from the screen. The peers of the items on the menu are 
also destroyed. </DL>
Miscellaneous methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean isTearOff () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>isTearOff()</tt> method returns 
<tt CLASS=literal>true</tt> if this <tt CLASS=literal>Menu</tt> 
is a tear-off menu, and <tt CLASS=literal>false</tt> 
otherwise. Once a menu is created, there is no way to change the tear-off 
setting. This method can return <tt CLASS=literal>true</tt> 
even on platforms that do not support tear-off menus. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public String paramString () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>paramString()</tt> method of 
<tt CLASS=literal>Menu</tt> should be protected like 
other <tt CLASS=literal>paramString()</tt> methods. 
However, it is public so you have access to it. When you call the <tt CLASS=literal>toString()</tt> 
method of a <tt CLASS=literal>Menu</tt>, the default 
<tt CLASS=literal>toString()</tt> method of <tt CLASS=literal>MenuComponent</tt> 
is called. This in turn calls <tt CLASS=literal>paramString()</tt>, 
which builds up the string to display. At the <tt CLASS=literal>Menu</tt> 
level, the setting for <tt CLASS=literal>TearOff</tt> 
(from constructor) and whether or not it is the help menu (from <tt CLASS=literal>MenuBar.setHelpMenu()</tt>) 
for the menu bar are added. If the constructor for the <tt CLASS=literal>Menu</tt> 
was <tt CLASS=literal>new Menu (`File`)</tt>, the 
results of <tt CLASS=literal>toString()</tt> would 
be: </DL>
<DIV CLASS=screen>
<P>
<PRE>
java.awt.Menu [menu0,label=File,tearOff=false,isHelpMenu=false]
</PRE>
</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-10-SECT-5.2">Menu Events</A></h3>

<P CLASS=para>
A <tt CLASS=literal>Menu</tt> does not generate any 
event when it is selected. An event is generated when a <tt CLASS=literal>MenuItem</tt> 
on the menu is selected, as long as it is not another <tt CLASS=literal>Menu</tt>. 
You can capture all the events that happen on a <tt CLASS=literal>Menu</tt> 
by overriding <tt CLASS=literal>postEvent()</tt>. 

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch10_04.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch10_06.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>MenuItem</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>CheckboxMenuItem</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
